package com.administrator.customviewtest.view.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/2/4.
 */

public class TextView11 extends View {
    private Paint paint;
    private int textSize = 40;
    private Rect textBound;
    private char tempChar;
    private onWorldClick onWorldClick;

    public void setOnWorldClick(TextView11.onWorldClick onWorldClick) {
        this.onWorldClick = onWorldClick;
    }

    public TextView11(Context context) {
        this(context, null);
    }

    public TextView11(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextView11(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setTextSize(textSize);
        textBound = new Rect();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(textSize, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < 27; i++) {
            char a = (char) (i + 96);
            String text = String.valueOf(a);
            paint.getTextBounds(text, 0, text.length(), textBound);
            canvas.drawText(text, getMeasuredWidth() / 2 - textBound.width() / 2, (getMeasuredHeight() / 26) * i, paint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                char a = (char) (event.getY() / (getMeasuredHeight() / 26) + 97);
                Log.e("ssss", event.getY() + " " + String.valueOf(a));
                if (a < 'a' || a > 'z') {
                    return false;
                }
                onWorldClick.onClick(String.valueOf(a));
                tempChar = a;
                setBackgroundColor(Color.GRAY);
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                char a = (char) (event.getY() / (getMeasuredHeight() / 26) + 97);
                if (a < 'a' || a > 'z') {
                    return false;
                }
                if (tempChar != a) {
                    Log.e("ssss", event.getY() + " " + String.valueOf(a));
                    tempChar = a;
                }
                onWorldClick.onClick(String.valueOf(a));
                break;
            }
            case MotionEvent.ACTION_UP: {
                setBackgroundColor(Color.BLACK);
                break;
            }
        }
        return true;
    }

    public interface onWorldClick {
        void onClick(String s);
    }
}
